//验证日期是否符合规范如果符合就是true不符合就是false
function isTime(date) {
    let result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (result === null) {
        return false;
    }
    result[1] = parseInt(result[1]);
    result[3] = parseInt(result[3]);
    result[4] = parseInt(result[4]);
    let d = new Date(result[1], result[3] - 1, result[4]);
    return (d.getFullYear() === result[1] && (d.getMonth() + 1) === result[3] && d.getDate() === result[4]);
}

//如果时间正确返回true如果时间错误返回false
function checkTime(time) {
    if (time.indexOf("~") !== -1) {
        let firstTime = time.split("~")[0];
        let lastTime = time.split("~")[1];
        if ((firstTime.length === 4 || firstTime.length === 5) && (lastTime.length === 4 || lastTime.length === 5) && firstTime.endsWith(":00") && lastTime.endsWith(":00") && !isNaN(firstTime.slice(0, -3)) && !isNaN(lastTime.slice(0, -3))) {
            firstTime = parseInt(firstTime.slice(0, -3));
            lastTime = parseInt(lastTime.slice(0, -3));
            return ((firstTime < lastTime) && firstTime >= 9 && firstTime < 22 && lastTime > 9 && lastTime <= 22);
        } else {
            return false;
        }
    } else {
        return false;
    }
}

//输入正确返回true输入错误返回false
function checkUserInput(ReservationArray) {
    let allspace = ["A", "B", "C", "D"];
    if (ReservationArray.length === 4) {
        let user = ReservationArray[0];
        let date = ReservationArray[1];
        let time = ReservationArray[2];
        let space = ReservationArray[3];
        return (isTime(date) && allspace.indexOf(space) !== -1 && user && checkTime(time));
    } else if (ReservationArray.length === 5) {
        let user = ReservationArray[0];
        let date = ReservationArray[1];
        let time = ReservationArray[2];
        let space = ReservationArray[3];
        let cencel = ReservationArray[4];
        return (cencel === "C" && isTime(date) && allspace.indexOf(space) !== -1 && user && checkTime(time));
    } else {
        return false;
    }
}

//改变用户的输入，然后返回一个对象
function changeData(ReservationArray) {
    let obj = {};
    let user = ReservationArray[0];
    let date = ReservationArray[1];
    let time = ReservationArray[2];
    let space = ReservationArray[3];
    if (ReservationArray.length === 5) {
        obj.data = {
            user: user,
            date: date,
            time: time,
            space: space
        };
        obj.method = "cancel";
        return obj;
    } else {
        obj.data = {
            user: user,
            date: date,
            time: time,
            space: space
        };
        obj.method = "booking";
        return obj;
    }
}

//验证用户输入的主函数
//如果用户输入的不符合要求就返回提示字符串
function isCorrect(Reservation) {
    if (Reservation.indexOf(' ') !== -1) {
        let ReservationArray = Reservation.split(" ");
        if (ReservationArray.length === 4 || ReservationArray.length === 5) {
            if (ReservationArray.indexOf(" ") === -1) {
                if (checkUserInput(ReservationArray)) {
                    return changeData(ReservationArray);
                } else {
                    return "Error: the booking is invalid!";
                }
            } else {
                return "Error: the booking is invalid!";
            }
        } else {
            return "Error: the booking is invalid!";
        }
    } else {
        return "Error: the booking is invalid!";
    }
}

module.exports.isCorrect = isCorrect;